/**
 * Light-commons Project
 * http://light-commons.googlecode.com
 * Copyright (C) 2008 Jason Green
 * email: guileen@gmail.com
 *
 * License: Apache License 2.0 
 * (http://www.apache.org/licenses/LICENSE-2.0)
 *
 */
package org.lightcommons.template.commontemplate;

import java.io.IOException;

import org.commontemplate.core.Source;
import org.commontemplate.engine.Engine;
import org.commontemplate.standard.ConfigurationSettings;
import org.commontemplate.standard.loader.AbstractSourceLoader;
import org.commontemplate.standard.loader.StringSource;
import org.commontemplate.tools.PropertiesConfigurationLoader;
import org.lightcommons.resource.ResourceLoader;
import org.lightcommons.template.AbstractTemplateFactory;
import org.lightcommons.template.Template;

/**
 * @author gl
 * @since Jun 16, 2008
 */
public class CommonTemplateFactory extends AbstractTemplateFactory{
	private ConfigurationSettings config;
//	private TemplateParser templateParser;
//	private ContextFactory contextFactory;
	private Engine engine;
//	private TemplateEngine templateEngine;
	public static final String STANDARD_CONFIG_PATH=PropertiesConfigurationLoader.STANDARD_CONFIG_PATH;
	public static final String WEB_CONFIG_PATH="org/commontemplate/tools/web/commontemplate.properties";
	
	public CommonTemplateFactory(ConfigurationSettings config) {
		init(config);
	}
	
	private void init(ConfigurationSettings config){
		this.config = config;
		this.engine = new Engine(config);
//		this.templateEngine = new TemplateEngine(config);
	}
	
	/**
	 * 
	 */
	public CommonTemplateFactory() {
		this(PropertiesConfigurationLoader.loadStandardConfiguration());
	}
	
	/**
	 * 
	 * @param propertiesPath
	 */
	public CommonTemplateFactory(String propertiesPath){
		this(PropertiesConfigurationLoader.loadConfiguration(propertiesPath));
	}

	@Override
	public Template compileSource(String name, String source)
			throws IOException {
		return new CommonTemplateFacade(engine.parseTemplate(name, source),engine);
	}
	
	@Override
	protected void initResourceLoader(ResourceLoader resourceLoader) {
		config.setSourceLoader(new CommontemplateSourceLoader(resourceLoader));
		init(config);		
	}
	
	static class CommontemplateSourceLoader extends AbstractSourceLoader{
		private ResourceLoader resourceLoader;
		
		public CommontemplateSourceLoader(ResourceLoader resourceLoader) {
			super();
			this.resourceLoader = resourceLoader;
		}

		@Override
		protected Source loadResource(String path, String name, String encoding)
				throws IOException {
			return new  StringSource(
					name, resourceLoader.getResource(path).getText(encoding));
		}
		
	}
	
//	@Override
//	protected void initResourceLoader(ResourceLoader loader) {
//		config.setResourceLoader(new CommonTemplateLoader(loader));
//		init(config);
//	}
//	
//	@Override
//	public Template compileSource(String name, String source) throws ParsingException, IOException {
//		try{
//			
//			Source source = loadSource(name, locale, encoding);
//			return persistentTemplate(source);
//			templateEngine.parseTemplate(source);
//			return new CommonTemplateFacade(templateEngine.parseTemplate(new StringResource(name,source)),engine);
//		}catch(Exception e){
//			throw new LightCommonsException("can't compile \""+name+"\" cause of :"+e.getMessage()+"\n source:\n"+source,e);
//		}
//	}
//
//	private static class CommonTemplateResource extends AbstractResource{
//		private org.lightcommons.resource.Resource resource;
//		
//		public CommonTemplateResource(org.lightcommons.resource.Resource resource,String name,String encoding) {
//			super(name, encoding);
//			Assert.notNull(resource);
//			this.resource = resource;
//		}
//
//		@Override
//		protected InputStream getInputStream() throws IOException {
//			return resource.getInputStream();
//		}
//
//		@Override
//		public long getLastModified() {
//			return 0;
//		}
//		
//	}
//	
//	private static class CommonTemplateLoader extends AbstractResourceLoader{
//		private ResourceLoader loader;
//		
//		public CommonTemplateLoader(ResourceLoader loader) {
//			super();
//			this.loader = loader;
//		}
//
//		@Override
//		protected Resource loadResource(String path, String name,
//				String encoding) throws IOException {
//			org.lightcommons.resource.Resource resource = loader.getResource(path);
//			Assert.notNull(resource,"can't load resource of '"+path+"'");
//			return new CommonTemplateResource(resource,name,encoding);
//		}
//		
//	}
}
